package common.massert;


import com.mida.tool.common.exception.MassertException;
import com.mida.tool.common.result.RetMsg;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * Created by yutao on 2017/4/20.
 */
public class MRAssert {

    public static void massertIsEmpty(Object obj, RetMsg retMsg) throws MassertException {

        if (!objectIsEmpty(obj)) {
            throw new MassertException(retMsg);
        }
    }
    public static void massertNotEmpty(Object obj, RetMsg retMsg) throws MassertException{

        if (objectIsEmpty(obj)) {
            throw new MassertException(retMsg);
        }
    }

    private static boolean objectIsEmpty(Object obj) {
        if (obj == null) {
            return true;
        }

        if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        }
        if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        }
        if (obj instanceof Collection) {
            return ((Collection<?>) obj).isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map<?, ?>) obj).isEmpty();
        }
        // else
        return false;
    }
}